package uit.edu.vn.portal.elearning.course;

import java.io.IOException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uit.edu.vn.portal.elearning.helper.SakaiWSHelper;
import uit.edu.vn.portal.elearning.logic.SakaiWSLogic;
import uit.edu.vn.portal.elearning.utils.Utils;
import uit.edu.vn.portal.elearning.models.course.Site;

import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.PortletURLFactoryUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class CoursePortlet
 */
public class CoursePortlet extends MVCPortlet {

	private final Log log = LogFactory.getLog(getClass().getName());
	
	private String adminUserName;
	private String adminPassword;
	private String loginUrl;
	private String scriptUrl;
	private String serviceUrl;
	private String sakaiUrl;
	private SakaiWSLogic logic;
	
    /**
     * Default constructor. 
     */
    public CoursePortlet() {
    
    }
  
    @Override
    public void init(PortletConfig config) throws PortletException {
    	// TODO Auto-generated method stub
    	super.init(config);
    	
    	log.info("Start CoursePortlet init()");

    	//get params
    	adminUserName = config.getInitParameter("sakai.admin.username");
    	adminPassword = config.getInitParameter("sakai.admin.password");
    	loginUrl = config.getInitParameter("sakai.ws.login.url");
    	scriptUrl = config.getInitParameter("sakai.ws.script.url");
    	serviceUrl = config.getInitParameter("sakai.ws.service.url");
    	sakaiUrl = config.getInitParameter("sakai.server.url");
    	log.info("End CoursePortlet init()");
    }
    
    @Override
    public void doView(RenderRequest renderRequest,
    		RenderResponse renderResponse) throws IOException, PortletException {
    	// TODO Auto-generated method stub
    	initSakaiWSLogic(renderRequest);
    	
    	List<Site> sites = SakaiWSHelper.getAllSitesForUser(logic);
    	List<Site> joinable_sites = SakaiWSHelper.getAllJoinableSitesForUser(logic);
    	
    	renderRequest.setAttribute("sites", sites);
    	renderRequest.setAttribute("joinable_sites", joinable_sites);
    	renderRequest.setAttribute("userid", Utils.getAuthenUsername(renderRequest));
    	renderRequest.setAttribute("sakaiUrl", this.sakaiUrl);
    	/*
    	String result = SakaiWSHelper.addNewSite(logic, 
    			"abcd-1234", 
    			"ABCD 1234", 
    			"ABCD 1234", 
    			"gdgfds", 
    			"asdf", 
    			"sdfasdf", 
    			"true", 
    			"Student", 
    			"true", 
    			"false", 
    			"uit-oln", 
    			"course");
    	
		String result_page = SakaiWSHelper.addNewPageToSite(logic, "abcd-1234", "Home", 1);
		log.info("result_page : " + result_page);
		
		String result_tool = SakaiWSHelper.addNewToolToPage(logic, "abcd-1234", "Home", "Home", "home", "0,0");
		log.info("result_tool : " + result_tool);
    		
    	result_page = SakaiWSHelper.addNewPageToSite(logic, "abcd-1234", "Site Info", 0);
		log.info("result_page : " + result_page);
    		
		result_tool = SakaiWSHelper.addNewToolToPage(logic, "abcd-1234", "Site Info", "Site Info", "sakai.siteinfo", "0,0");
		log.info("result_tool : " + result_tool);
    	*/
    	super.doView(renderRequest, renderResponse);
    }
    
    private void initSakaiWSLogic(RenderRequest renderRequest){
		   logic = new SakaiWSLogic();
		   logic.setAdminUsername(adminUserName);
		   logic.setAdminPassword(adminPassword);
		   logic.setEid(Utils.getAuthenUsername(renderRequest));
		   logic.setLoginUrl(loginUrl);
		   logic.setScriptUrl(scriptUrl);
		   logic.setServiceUrl(serviceUrl);
	}
    
    public void JoinSite(ActionRequest request, ActionResponse response){
		log.info("vao day roi");
		
		String site_join_id = ParamUtil.getString(request, "join_site_id");
		String userid = ParamUtil.getString(request, "userid");
		String result = SakaiWSHelper.addMemberToSiteWithRole(logic, site_join_id, userid, "Student");
		log.info(result);
		log.info(site_join_id);
    }

    public void addNewSite(ActionRequest request, ActionResponse response){
    	
    	//get course info from request
    	String siteid = Utils.generateSiteId();
    	String title = ParamUtil.getString(request,"title");
    	String description = ParamUtil.getString(request,"description");
    	boolean joinable = ParamUtil.getBoolean(request, "joinable");
    	String shortdesc = description;
    	String iconurl = "/library/icon/engineering.gif";
    	String infourl = "";
    	String joinerrole = "Student";
    	boolean published = ParamUtil.getBoolean(request, "published");
    	boolean publicview = ParamUtil.getBoolean(request, "publicview");
    	String skin = "uit-oln";
    	String type = ParamUtil.getString(request,"type");
    	//end
    	
    	try{
    		String result = SakaiWSHelper.addNewSite(logic, siteid, title, description, shortdesc, iconurl, infourl, String.valueOf(joinable), joinerrole, String.valueOf(published), String.valueOf(publicview), skin, type);
    		//if( result == "success" ){
    				//home
    				SakaiWSHelper.addNewPageToSite(logic, siteid, "Home", 1);
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Home", "Home", "home", "0,1");
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Home", "Site Information Display", "sakai.iframe.site", "1,0");
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Home", "Recent Announcements", "sakai.synoptic.announcement", "0,1");
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Home", "Calendar", "sakai.summary.calendar", "1,1");
    				
    				//Syllabus
    				SakaiWSHelper.addNewPageToSite(logic, siteid, "Syllabus", 0);
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Syllabus", "Syllabus", "sakai.syllabus", "0,0");
    				
    				//Site info
    				SakaiWSHelper.addNewPageToSite(logic, siteid, "Site Info", 0);
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Site Info", "Site Info", "sakai.siteinfo", "0,0");
    				
    				//Announcement 
    				SakaiWSHelper.addNewPageToSite(logic, siteid, "Announcements", 0);
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Announcements", "Announcements", "sakai.announcements", "0,0");
    				
    				//Assignment 
    				SakaiWSHelper.addNewPageToSite(logic, siteid, "Assignments", 0);
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Assignments", "Assignments", "sakai.assignment.grades", "0,0");
    				
    				//Schedule
    				SakaiWSHelper.addNewPageToSite(logic, siteid, "Schedule", 0);
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Schedule", "Schedule", "sakai.schedule", "0,0");
    				
    				//Test and Quiz
    				SakaiWSHelper.addNewPageToSite(logic, siteid, "Test & Quiz", 0);
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Test & Quiz", "Test & Quiz", "sakai.samigo", "0,0");
    				
    				//Gradebook
    				SakaiWSHelper.addNewPageToSite(logic, siteid, "Gradebook", 0);
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Gradebook", "Gradebook", "sakai.gradebook.tool", "0,0");
    				
    				//Site Stats
    				SakaiWSHelper.addNewPageToSite(logic, siteid, "Site Stats", 0);
    				SakaiWSHelper.addNewToolToPage(logic, siteid, "Site Stats", "Site Stats", "sakai.sitestats", "0,0");
    				
    		//}
    	}
    	catch (Exception e) {
			log.info("Error : " + e.getMessage());
		}
    	
    }
}
